import Vue from 'vue'
import Vuex from 'vuex'
import userModule from "./user"
import deliverModule from "./deliver"
import commissionModule from "./commission"
import couponModule from "./coupon"
import priceModule from "./price"
import transferModule from "./transfer"
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    id: /item\.jd\.com\/(.*)\.html/.exec(location.href) && /item\.jd\.com\/(.*)\.html/.exec(location.href)[1],
    productInfo: { shopInfo: {}, unionGoods: [[{}]], unionRecommendGoods: [] },
    priceTotal: {},
    JTTProductInfo: { categoryInfo: {}, shopInfo: {}, couponInfo: { couponList: [] }, commissionInfo: {} }
  },
  mutations: {
    setPriceTotal(state, palyload) {
      state.priceTotal = palyload
    },
    setProductInfo(state, palyload) {
      state.productInfo = palyload
    },
    setJTTProductInfo(state, palyload) {
      state.JTTProductInfo = palyload
    }
  },
  actions: {
    //获取价格数据
    getPrice({ state, commit }) {
      return new Promise((resolve, reject) => {
        chrome.runtime.sendMessage({
          type: "custom_header_post",
          url: "/ajaxpro/Susie.WebTool.QueryGoods_WX,Susie.WebTool.ashx",
          baseURL: "http://jd1988.cn",
          data: {
            sku: state.id
          }
        }, function (result) {
          let echartsData = JSON.parse(result.replace(/new Date\(Date.UTC\((.*?)\)\),/g, function (rs, $1, $2, offset, source) {
            //arguments中的每个元素对应一个参数
            var dateList = $1.split(",")
            var date = new Date(Date.UTC.apply(null, dateList))
            var year = date.getFullYear()
            var month = (date.getMonth() + 1 + "").length == 1 ? '0' + (date.getMonth() + 1 + "") : (date.getMonth() + 1 + "")
            var day = (date.getDate() + "").length == 1 ? '0' + (date.getDate() + "") : (date.getDate() + "")
            return "\"" + year + "-" + month + "-" + day + "\","
          }))
          var _data = echartsData.value.RstValue
          var xAxisData = _data.map(function (item) {
            return item.Date
          })
          var series_1 = _data.map(function (item) {
            return item.Price_JD
          })
          var series_2 = _data.map(function (item) {
            return item.Price_Per_Final
          })
          var series_3 = _data.map(function (item) {
            return item.Price_Avg30
          })
          commit("setPriceTotal", { xAxisData, series_1, series_2, series_3 })
          resolve({ xAxisData, series_1, series_2, series_3 })
        })
      })

    },
    //高效转链
    efficientTransfer({ state, commit, dispatch }) {
      const { isPinGou, requestId, planId } = state.productInfo.unionGoods[0][0]
      return new Promise((resolve, reject) => {
        chrome.runtime.sendMessage({
          type: "custom_header_post",
          url: "/api/receivecode/getCode",
          baseURL: "https://union.jd.com",
          data: {
            data: {
              isPinGou,
              isSmartGraphics: 0,
              materialId: state.id,
              materialType: 0,
              needAutoVerifyPlan: [],
              planId,
              promotionType: 15,
              receiveType: "cps",
              requestId,
              wareUrl: location.href.replace("html#/", "html")
            }
          }
        }, function (result) {
          resolve(result.data.data)
        });
      })
    },
    //京推推联盟搜索
    jttLMSearch({ commit, state, dispatch }) {
      chrome.runtime.sendMessage({
        type: "custom_header",
        url: "/api/jd_goods_query",
        baseURL: "http://japi.jingtuitui.com",
        params: {
          appid: state.userModule.appId,
          appkey: state.userModule.appKey,
          skuIds: state.id
        }
      }, function (res) {
        if (res.result.goods) {
          commit("setJTTProductInfo", res.result.goods[0])
        }
      });
    },
    //京东联盟 联盟搜索
    lmSearch({ commit, state, dispatch }) {
      chrome.runtime.sendMessage({
        type: "custom_header_post",
        url: "/api/goods/search",
        baseURL: "https://union.jd.com",
        data: {
          page: 1,
          data: {
            key: state.id
          }
        }
      }, function (result) {
        if (result.data) {
          if (result.data.unionGoods[0][0].skuId == state.id) {
            commit("setProductInfo", result.data)
            commit("userModule/setLogin", true)
          }
        }
        else {
          commit("userModule/setLogin", false)
        }
      });
    }
  },
  modules: {
    userModule,
    deliverModule,
    commissionModule,
    couponModule,
    priceModule,
    transferModule
  }
})
